package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin/product")
//@CrossOrigin    //解决跨域
public class SpuManageController {

    //调用服务层方法
    @Autowired
    private ManageService manageService;

    //分页
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
//                                 Long category3Id,
//                                 @RequestParam Long category3Id,
//                                 HttpServletRequest request
                                 SpuInfo spuInfo){
        // 创建分页类
        Page ipage = new Page(page,limit);
        //调用服务层方法
        IPage SpuInfoPage = manageService.getSpuInfoPage(ipage,spuInfo);
        //返回数据
        return Result.ok(SpuInfoPage);
    }

    //获取所有的销售属性数据
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        //查询所有的销售属性集合
        List<BaseSaleAttr> baseSaleAttrList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //保存spu
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //调用服务层
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    // /admin/product/spuImageList/{spuId}
    //根据souId获取图片集合
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        //调用方法
        List<SpuImage> spuImageList= manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    // /admin/product/spuSaleAttrList/{spuId}
    //根据spuId获取销售属性集合
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        //调用服务层方法
        List<SpuSaleAttr> spuSaleAttrList = manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
}
